package bookstore.dao;

import bookstore.idao.ICategoryDao;
import bookstore.model.Category;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CategoryDao implements ICategoryDao {

    @Override
    public void update(Category bs_category) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = MySQLHelper.connect();
            String sql = "update bs_category set cat_name=? where cat_id=?";
            ps = con.prepareStatement(sql);
            ps.setString(1, bs_category.getName());
            ps.setInt(2, bs_category.getId());
            ps.executeUpdate();
        } catch (Exception e) {
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
    }

    @Override
    public void insert(Category admin) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = MySQLHelper.connect();
            String sql = "insert into bs_category(cat_name) values(?)";
            ps = con.prepareStatement(sql);
            ps.setString(1, admin.getName());
            ps.executeUpdate();
        } catch (Exception e) {
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
    }
@Override
    public void updatePassword(Category admin){
        
    }
     @Override
    public void updateMessage(Category admin){
        
    }
    @Override
    public void delete(Integer id) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = MySQLHelper.connect();
            String sql = "delete from bs_category where cat_id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (Exception e) {
        } finally {
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
    }

    @Override
    public Category findById(Integer id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Category bs_category = null;
        try {
            con = MySQLHelper.connect();
            String sql = "select * from bs_category where cat_id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                bs_category = new Category();
                bs_category.setId(rs.getInt("cat_id"));
                bs_category.setName(rs.getString("cat_name"));
            }
        } catch (Exception e) {
        } finally {
            MySQLHelper.closeResult(rs);
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return bs_category;
    }

    @Override
    public List<Category> findAll() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Category> list = new ArrayList<Category>();
        try {
            con = MySQLHelper.connect();
            String sql = "select * from bs_category";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            Category bs_category = null;
            while (rs.next()) {
                bs_category = new Category();
                bs_category.setId(rs.getInt("cat_id"));
                bs_category.setName(rs.getString("cat_name"));
                list.add(bs_category);
            }
        } catch (Exception e) {
        } finally {
            MySQLHelper.closeResult(rs);
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return list;
    }

    @Override
    public int findAllCount() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            con = MySQLHelper.connect();
            String sql = "select count(*) as count from bs_category";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                count = rs.getInt("count");
            }
        } catch (Exception e) {
        } finally {
            MySQLHelper.closeResult(rs);
            MySQLHelper.closePreparedStatement(ps);
            MySQLHelper.closeConneciton(con);
        }
        return count;
    }
}
